home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvi2qms / qmsf.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  5KB  |  217 lines

  1.  
  2. /*
  3.  *    qmsf.c - output filter for qms printers
  4.  *
  5.  *    Copyright 1985 Massachusetts Institute of Technology
  6.  *    Author: CJL@OZ
  7.  *
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <signal.h>
  13. #include <sys/types.h>
  14. #include "util.h"
  15. #include "fonts.h"
  16.  
  17. /* \122 = 'R'. The MOBY_RESET string is constructed this way so 
  18.  * this source text can be printed out on a QMS printer without resetting
  19.  * the printer. */
  20. #define MOBY_RESET    "\122eSeTrEsEtReSeT"
  21. #define QUIC_ON        "\r^PY^-\r"
  22. #define QUIC_OFF    "\r^-^PN^-\r"
  23. #define DEFAULT_FONT    "^IS%05.5d^-"
  24. #define DELETE_FONTS    "^DFEA^G^-"
  25. #define CLEAR_OVERLAYS    "^DOC^Z^-"
  26. #define SYNTAX        "^ISYNTAX%1.1d%1.1d%1.1d%1.1d%1.1d^-"
  27. #define PORTRAIT    "^IOP^-"
  28. #define V_MARGINS    "^IMV%05.5d%05.5d^-"
  29. #define H_MARGINS    "^IMH%05.5d%05.5d^-"
  30. #define TRAY        "^IF%1.1d%1.1d%1.1d%1.1d^-"
  31. #define CHAR_SP        "^IC%04.4d^-"
  32. #define LINE_SP        "^IL%04.4d^-"
  33.  
  34. extern char *rindex();
  35. char *programname;
  36. long charvec[4];
  37. int devwidth,texwidth;
  38. int ready = 0;
  39. int length = 66;
  40. int width = 94;
  41. int charpos,charcol,onfirstpage;
  42. int qms2400 = 0;
  43.  
  44. init_qms()
  45. {
  46.   int i;
  47.   char *s = PXLDIR;
  48.  
  49.   /* Reset the printer. 
  50.    * We can assume nothing about the current state of the printer.
  51.    * So first set the printer to a known state.
  52.    */
  53.   printf(MOBY_RESET);        /* MOBY reset */
  54.   printf(QUIC_ON);        /* Turn on QUIC */
  55.   /* printf(DELETE_FONTS); */    /* Delete all downloaded fonts */
  56.   printf(CLEAR_OVERLAYS);    /* Clear all overlays */
  57.   printf(MOBY_RESET);        /* MOBY reset again */
  58.  
  59.   printf(QUIC_ON);        /* Turn on QUIC */
  60.   printf(SYNTAX,0,0,0,0,0);    /* Ensure default SYNTAX */
  61.   printf(PORTRAIT);        /* Portrait page orientation */
  62.   printf(V_MARGINS,250,11000);    /* Vertical margins */
  63.   printf(H_MARGINS,890,8500);    /* Horizontal margins */
  64.   printf(CHAR_SP,0);        /* Turn off char spacing */
  65.   printf(LINE_SP,620);        /* Vertical spacing */
  66.   printf(DEFAULT_FONT,10);    /* Select the font I like */
  67.   printf(SYNTAX,0,0,0,1,0);    /* Ensure default SYNTAX */
  68.   if (qms2400)
  69.     printf(TRAY,0,0,0,1);    /* Take first page from tray 2 */
  70.   f_init(stdout,programname,&s,1,10,157876,1000,0);
  71.   for (i = ' ';i < 0377;i++) charvec[i / 32] |= (1 << (i % 32));
  72.   f_define_font(1,0,"","amtt10",1095,717619,0);
  73.   i = 1;
  74.   f_newpage(&i,charvec,1);
  75.   f_use_font(1,&i);
  76.   onfirstpage = ready = 1;
  77.   charpos = charcol = 0;
  78. };
  79.  
  80.  
  81. dev_print_log()
  82. {
  83. }
  84.  
  85. term_qms()
  86. {
  87.   if (ready) {
  88.     dochar('\f');
  89.     ready = 0;
  90.   }
  91.   printf(QUIC_OFF);
  92.   fflush(stdout);
  93. };    
  94.  
  95. main(argc, argv) 
  96.     int argc;
  97.     char *argv[];
  98. {
  99.   register int ch;
  100.   int i;
  101.  
  102.   programname = argv[0];
  103.   if (!access("qms2400",0)) qms2400 = -1;
  104.   for (i = 0; i < argc; i++) {
  105.     if (argv[i][0] = '-') switch (argv[i][1]) {
  106.     case 'w':
  107.       sscanf(argv[i]+2,"%d",&width);
  108.       break;
  109.  
  110.     case 'l':
  111.       sscanf(argv[i]+2,"%d",&length);
  112.       break;
  113.     }
  114.   }
  115.   while (!ferror(stdin) && (ch = getchar()) != EOF) switch (ch) {
  116.   case '\031':
  117.     /*
  118.      * lpd needs to use a different filter to
  119.      * print data so stop what we are doing and
  120.      * wait for lpd to restart us.
  121.      */
  122.     if ((ch = getchar()) == '\1') {
  123.       term_qms();
  124.       kill(getpid(), SIGSTOP);
  125.       break;
  126.     } else {
  127.       ungetc(ch, stdin);
  128.       ch = '\031';
  129.     }
  130.   default:
  131.     dochar(ch);
  132.   }
  133.   term_qms();
  134.   exit(0);
  135. }
  136.  
  137. dochar (ch)
  138. int ch;
  139. {
  140.   int i;
  141.  
  142.   if (ch < ' ') 
  143.     switch (ch) {
  144.     case '\b':
  145.       if (!ready) break;
  146.       if (charpos) { 
  147.     charpos--;
  148.     printf("^TR-%04.4d",devwidth);
  149.       }
  150.       break;
  151.  
  152.     case '\t':
  153.       if (!ready) init_qms();
  154.       for (i = 8 - charpos%8; i > 0; i--) dochar(' ');
  155.       break;
  156.  
  157.     case '\n':
  158.       if (!ready) init_qms();
  159.       if (++charcol >= length) dochar('\f');
  160.       else {
  161.     putchar('\r');
  162.     putchar('\n');
  163.     charpos = 0;
  164.       }
  165.       break;
  166.  
  167.     case '\r':
  168.       if (!ready) break;
  169.       if (charpos != 0) {
  170.     charpos = 0;
  171.     putchar('\r');
  172.       }
  173.       break;
  174.  
  175.     case '\f':
  176.       if (!ready || ((charpos == 0) && (charcol == 0))) break;
  177.       charpos = 0;
  178.       charcol = 0;
  179.       putchar('\f');
  180.       i = 1;
  181.       f_newpage(&i,charvec,1);
  182.       f_use_font(1,&i);
  183.       fflush(stdout);
  184.       if (qms2400 && onfirstpage) {
  185.     printf(TRAY,2,0,0,0);    /* Take rest of paper from tray 1 */
  186.       }
  187.       onfirstpage = 0;
  188.       break;
  189.     
  190.     default:
  191.       if (!ready) init_qms();
  192.       dochar('^'); dochar('\b'); dochar('|');
  193.       dochar(ch+'A'-001);
  194.       break;
  195.     } else {
  196.       if (!ready) init_qms();
  197.       if (++charpos > width) {
  198.     putchar('!');
  199.     dochar('\n');
  200.     dochar(ch);
  201.       } else {
  202.     f_use_char(ch,&texwidth,&devwidth);
  203.     switch (ch) {
  204.     case ' ':
  205.       printf("^TR%04.4d",devwidth);
  206.       break;
  207.       
  208.     case '^':
  209.       putchar(ch);
  210.  
  211.     default:
  212.       putchar(ch);
  213.     }
  214.       }
  215.     }
  216. }
  217.